<?php
/*
* IKPHP爱客网 安装程序 @copyright (c) 2012-3000 IKPHP All Rights Reserved @author 小麦
* @Email:ikphp@sina.cn
* @小麦 关注 2014年8月9日12:11:41
*/
namespace IKPHP\Apps\Group\Models;

use IKPHP\Common\Models\BaseModel;
use IKPHP\Apps\Group\Models\GroupUsers;
use IKPHP\Common\Models\User;

class Group extends BaseModel {
	
	public $groupid;
	public $userid;
	public $cateid;
	public $groupname;	
	public $groupname_en;
	
	public function getSource() {
		return IK . "group";
	}
    /* 用户模型自动完成 */	
    public function beforeCreate()
    {
    	$this->count_topic 	= 0;
        $this->count_topic_today 	= 0;
        $this->count_user	= 0;
        $this->joinway	= 0;
        $this->role_leader	= '组长';
        $this->role_admin	= '管理员';
        $this->role_user	= '成员'; 
        
        $this->isrecommend	= 0; 
        $this->isopen	= 0; 
        $this->isaudit	= 0; //是否要审核 0 不审核 1 审核
        $this->ispost	= 0; 
        $this->isshow = 0;
        
    }	
 	//小组名称唯一检查
 	public static function gname_exists($groupname){
 		$res = self::count("groupname = '$groupname'"); 
 		return $res > 0 ? true : false;
 	}
 	public function afterCreate(){
 		$groupuser = new GroupUsers();
 		//绑定成员
		$data ['userid'] = $this->userid;
		$data ['isadmin'] = 1;
		$data ['groupid'] = $this->groupid;
		$data ['addtime'] = time ();
		$groupuser->create($data);	
 	}
 	//获取一个小组
 	public static function getOneGroup($groupid){
 		$result = array();
 		$result = self::findFirst("groupid = '$groupid'");
 		if($result){
 			$result = $result->toArray();
 			if(!empty($result['groupicon'])){
 				$imgname = explode('.', $result['groupicon']);
 				$result['icon_48'] = attach('group/icon/'.$imgname[0].'_48_48.'.$imgname[1]);
 				$result['icon_60'] = attach('group/icon/'.$imgname[0].'_60_60.'.$imgname[1]);
 				$result['icon_org'] = attach('group/icon/'.$result['groupicon']);
 			}else{
 				$result['icon_48'] = $result['icon_60'] = $result['icon_org'] = C('ik_site_url') . 'static/images/group.jpg';
 			}
 			
 		}
 		return $result;
 	}
	//小组内最新加入的会员
	public static function getNewGroupUser($groupid,$limit=10){
		$where = array (
				'conditions'=>"groupid = '$groupid'",
				'order'=> 'addtime desc',
				'limit'=>$limit,
		); 	
		$results = GroupUsers::find($where); 
		$result = array();
		foreach($results->toArray() as $key=>$item){
			$result[] = User::getOneUser($item['userid']);
		}
		return $result;
	}
	// 某用户创建的小组 
	public static function getUserGroup($userid){
		$arrResult = array();
		$result = self::find("userid = '$userid'");
		if(count($result) > 0){
			foreach ($result->toArray() as $key=>$item){
				$arrResult[$key] = $item;
				if(!empty($item['groupicon'])){
	 				$imgname = explode('.', $item['groupicon']);
	 				$arrResult[$key]['icon_48'] = attach('group/icon/'.$imgname[0].'_48_48.'.$imgname[1]);
	 				$arrResult[$key]['icon_60'] = attach('group/icon/'.$imgname[0].'_60_60.'.$imgname[1]);
	 			}else{
	 				$arrResult[$key]['icon_48'] = $arrResult[$key]['icon_60'] = C('ik_site_url') . 'static/images/group.jpg';
	 			}				
			}
			
		}
		
		return $arrResult;		
	}	
	// 某用户加入的小组 不包括自己创建的小组
	public static function getUserJoinGroup($userid){
		$result = array();
		
		$myCreateGroup = self::getUserGroup($userid); 
		$arrGroup = array();
		if(is_array($myCreateGroup)){ 
			foreach($myCreateGroup as $key=>$item){ 
				$arrGroup[] = $item['groupid'];
			}
		}

		$strGroup = empty($arrGroup) ? '' : implode(',',$arrGroup); 
		
		if(empty($strGroup)){
			return $result;
		}
		
		$where['conditions'] = "userid = '$userid' AND groupid NOT IN ($strGroup) ";
		
		$result = GroupUsers::find($where); 
		if(count($result)>0){
			return $result->toArray();
		}
		
		return $result;		
	}
	/**
	 * 获取全站
	 * @param array $limit 
	 **/	
	public static function getGroupByCateid($where, $limit = '', $userid = 0){
		
		$arrResult = array();
		$map['conditions'] = $where;
		$map['order'] = 'count_topic DESC,count_topic_today DESC,count_user DESC,isrecommend DESC';
		!empty($limit)&&$map['limit'] = $limit;
		
		$res = self::find($map);
		if(count($res)>0){
			foreach ($res->toArray() as $key=>$item){
				    $arrResult[$key] = $item;
			 	if(!empty($item['groupicon'])){
	 				$imgname = explode('.', $item['groupicon']);
	 				$arrResult[$key]['icon_48'] = attach('group/icon/'.$imgname[0].'_48_48.'.$imgname[1]);
	 				$arrResult[$key]['icon_60'] = attach('group/icon/'.$imgname[0].'_60_60.'.$imgname[1]);
	 			}else{
	 				$arrResult[$key]['icon_48'] = $arrResult[$key]['icon_60'] = C('ik_site_url') . 'static/images/group.jpg';
	 			}
	 			if($userid > 0){
	 				$arrResult[$key]['isGroupUser'] = GroupUsers::isGroupUser($userid, $item['groupid']);
	 			}else{
	 				$arrResult[$key]['isGroupUser'] = false;
	 			}
			}
		}
		
		return $arrResult;	
	}
	public static  function countGroup($where){ 
		$count = self::count($where);
		return $count;
	}
	/**
	 * 获取值得加入的小组
	 * @param array $limit 
	 **/			
 	//获取一个小组
 	public static function getEssenceGroup($limit='',$userid=0){
		$arrResult = array();
		$map['conditions'] = "isshow=0 AND isaudit=0";
		$map['order'] = 'isrecommend DESC, count_user DESC,count_topic DESC,count_topic_today DESC';
		!empty($limit)&&$map['limit'] = $limit;
		
		$res = self::find($map);
		if(count($res)>0){
			foreach ($res->toArray() as $key=>$item){
				    $arrResult[$key] = $item;
			 	if(!empty($item['groupicon'])){
	 				$imgname = explode('.', $item['groupicon']);
	 				$arrResult[$key]['icon_48'] = attach('group/icon/'.$imgname[0].'_48_48.'.$imgname[1]);
	 				$arrResult[$key]['icon_60'] = attach('group/icon/'.$imgname[0].'_60_60.'.$imgname[1]);
	 			}else{
	 				$arrResult[$key]['icon_48'] = $arrResult[$key]['icon_60'] = C('ik_site_url') . 'static/images/group.jpg';
	 			}
	 			if($userid > 0){
	 				$arrResult[$key]['isGroupUser'] = GroupUsers::isGroupUser($userid, $item['groupid']);
	 			}else{
	 				$arrResult[$key]['isGroupUser'] = false;
	 			}
			}
		}
		
		return $arrResult;
 	}
	//获取推荐的小组
	public static function getRecommendGroup($limit=''){
		$map['columns'] = 'groupid';
		$map['conditions'] = "isrecommend=1";
		$map['order'] = 'groupid asc';
		!empty($limit)&&$map['limit'] = $limit;
		$arrGroup = self::find($map);
		if(count($arrGroup)>0){
			foreach($arrGroup->toArray() as $item){
				$result[] = self::getOneGroup($item['groupid']);
			}
		}
		return $result;
	} 	
}
